package com.guli.eduService.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonUtils.R;
import com.guli.commonUtils.vo.CourseVo;
import com.guli.eduService.entity.EduCourse;
import com.guli.eduService.entity.vo.PublishedCourseVO;
import com.guli.eduService.entity.vo.QueryCourseFormInfo;
import com.guli.eduService.entity.vo.SaveCourseFormInfo;
import com.guli.eduService.service.impl.EduCourseServiceImpl;
import com.guli.serviceBase.Entity.GuliExcepiton;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zxc
 * @since 2023-04-03
 */
@Api(description = "课程基本信息管理")
@RestController
@RequestMapping("/eduService/course")
//@CrossOrigin // 解决跨域问题
public class EduCourseController {

    @Autowired
    private EduCourseServiceImpl eduCourseService;

    
    // 根据查询条件，获取所有符合条件的课程数据 && 分页展示所有课程数据;
    // 需要注意的是，若是前端所传递的课程条件为空的话，则所查询到就是所有课程数据 && 分页展示所有课程数据;
    @ApiOperation(value = "带有筛选条件的分页课程列表")
    @PostMapping("ConditionQueryCourseInfo/{page}/{limitRecords}")
    public R getCourseInfoByConditions(
            @ApiParam(name = "page" , value = "当前页码" ,required = true)
            @PathVariable Long page,

            @ApiParam(name = "limitRecords" , value = "每页记录数" , required = true)
            @PathVariable Long limitRecords,

            @ApiParam(name = "queryCourseFormInfo" , value = "课程查询条件" , required = true)
            @RequestBody QueryCourseFormInfo queryCourseFormInfo){
        // 分页组件，经过查询后，所查询到的所有数据都会被存储到该Page对象中;
        Page<EduCourse> coursePage = new Page<>(page , limitRecords);
        // 调用该方法，对于所有课程信息进行条件查询后所得到的所有数据，都会被封装到Page对象中;
        // 后续，若是要获取条件查询后的课程数据，则直接去Page对象中获取即可;
        eduCourseService.conditionQueryCourseInfo(queryCourseFormInfo , coursePage);
        // 所查询到符合条件的记录总数;
        long total = coursePage.getTotal();
        // 获取当前页码下，所符合条件的课程数据;
        List<EduCourse> records = coursePage.getRecords();
        Map<String , Object> map = new HashMap<>();
        map.put("total" , total);
        map.put("records" , records);
        return R.ok().data(map);
    }

    @ApiOperation(value = "查询所有课程信息")
    @GetMapping("getAllCourseInfo")
    public R showCourseInfo(){
        // 即，不限制任何查询条件，所查询到的应该是所有课程信息;
        List<EduCourse> eduCourseList = eduCourseService.list(null);
        return R.ok().data("CourseList" , eduCourseList);
    }

    @ApiOperation(value = "添加课程基本信息")
    @PostMapping("addCourse")
    public R addCourseInfo(
            @ApiParam(name = "courseFormInfo" , value = "课程基本信息" , required = true)
            @RequestBody SaveCourseFormInfo courseFormInfo){
        // addCourseInfo方法 ：所返回的课程id是用于后续给该课程添加章节大纲;
        String courseId = eduCourseService.addCourseInfo(courseFormInfo);
        if(!StringUtils.isEmpty(courseId)){
            return R.ok().data("courseId" , courseId);
        }
        else {
            return R.error().data("error" , "添加课程失败~~~");
        }
    }

    // 用于回显课程基本信息;
    @ApiOperation(value = "获取当前课程基本信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(
            @ApiParam(name = "courseId" , value = "课程id",required = true)
            @PathVariable String courseId){
        SaveCourseFormInfo courseFormInfo = eduCourseService.getCourseVOInfo(courseId);
        return R.ok().data("courseInfo" , courseFormInfo);
    }

    // 用于修改某个课程的基本信息;
    @ApiOperation(value = "修改课程基本信息")
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(
            @ApiParam(name = "courseFormInfo" , value = "表单数据" ,required = true)
            @RequestBody SaveCourseFormInfo courseFormInfo){
        String courseId = eduCourseService.updateCourseInfo(courseFormInfo);
        return R.ok().data("courseId" , courseId);
    }

    // 根据课程id，联合多个数据库表，来查询该课程的详细信息;
    @ApiOperation(value = "查询课程详细信息")
    @GetMapping("queryPublishedCourseInfo/{courseId}")
    public R getPublishedCourseInfo(
            @ApiParam(name = "courseId" , value = "课程id" , required = true)
            @PathVariable String courseId){
        PublishedCourseVO publishedCourseInfo = eduCourseService.queryPulishedCourseInfo(courseId);
        return R.ok().data("publishedCourseInfo" , publishedCourseInfo);
    }

    // 根据课程id，对于课程状态（Draft-未发布）进行修改，修改成发布状态(Normal-已发布状态)
    @ApiOperation(value = "正式发布课程")
    @GetMapping("publishCourse/{courseId}")
    public R publishCourse(
            @ApiParam(name = "courseId" , value = "课程id" , required = true)
            @PathVariable String courseId){
        boolean flag = eduCourseService.updatePublishCourseInfo(courseId);
        if(!flag){
            return R.ok().success(false).data("Info" , "该课程已存在!!!");
        }
        return R.ok().data("publishResult" , "最终发布课程成功~~~");
    }

    // 根据课程id，删除课程的所有信息（包括课程基本信息，课程简介信息，课程章节信息，课程小节信息等等）
    @ApiOperation(value = "根据课程id，删除有关该课程的所有信息")
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(
            @ApiParam(name = "courseId" , value = "课程id" ,required = true)
            @PathVariable String courseId){
        eduCourseService.deleteAllCourseInfo(courseId);
        return R.ok();
    }

    @ApiOperation(value = "根据课程id，获取当前课程的基本信息")
    @GetMapping("getBasicCourseInfo/{courseId}")
    public CourseVo getBasicCourseInfo(
            @ApiParam(name = "courseId" , value = "课程id" , required = true)
            @PathVariable String courseId){
        CourseVo basicCourseInfo = eduCourseService.queryBasicCourseInfo(courseId);
        return basicCourseInfo;
    }
}

